/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.ui.tooltip {
	import com.googlecode.milib.MILibMain;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.service.singleton.MouseService;
	import com.googlecode.milib.util.MLMCUtil;
	import com.googlecode.milib.util.MLMathUtil;
	import com.googlecode.milib.vo.MLEvent;

	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.text.TextField;

	/**
	 * @author Marek Brun
	 */
	public class MCTooltip extends MLBroadcastClass {
		
		private var mlmc:MLMC;
		private var mc_bg : MovieClip;
		private var mc_triangle : MovieClip;
		private var mc_tf : TextField;
		private var align:Number=0;
		private var fromTriangleDistance:Number;
		private var container:DisplayObjectContainer;
		private var isVisible:Boolean;
		private var isStickToMouse:Boolean=true;
		private var ms:MouseService;		private var margin:Number=10;
		public var mc_triangle_width:Number;
		
		public function MCTooltip(mc:MovieClip, container:DisplayObjectContainer=null) {
			mlmc=MLMC.forInstance(mc);
			mc_bg=MovieClip(mc.getChildByName('bg'));
			mc_triangle=MovieClip(mc.getChildByName('triangle'));
			mc_triangle.gotoAndStop(1);
			mc_triangle.x=mc_triangle.y=0;
			mc_tf=TextField(mc.getChildByName('tf'));
			fromTriangleDistance=-mc_bg.y-mc_bg.height;
			mc_triangle_width=mc_triangle.width;
			mc.mouseEnabled=false;			mc.mouseChildren=false;
			
			this.container=container ? container : MILibMain.getInstance().getRoot();
			ms=MouseService.getInstance();
			
			if(mc.parent){
				mc.parent.removeChild(mc);
			}
			
		}
		
		public function setTriangleFrame(frame:uint):void {
			mc_triangle.gotoAndStop(frame);
		}

		public function setIsVisible(visible:Boolean):void {
			if(isVisible==visible){ return; }
			isVisible=visible;
			if(isVisible) {
				container.addChildAt(mc, container.numChildren);
				if(isStickToMouse){
					ms.addEventListener(ms.event_Move, onMouseMove);
				}
			}else{
				mc.parent.removeChild(mc);
				ms.removeEventListener(ms.event_Move, onMouseMove);
			}
		}
		
		public function setIsStickToMouse(stickToMouse:Boolean):void {
			if(isStickToMouse==stickToMouse){ return; }
			isStickToMouse=stickToMouse;
		}
		
		public function setPosition(x:Number, y:Number):void {
			mlmc.mc.x=x;			mlmc.mc.y=y;
		}
		
		public function setPositionFromMC(x:Number, y:Number, fromMC:MovieClip):void {
			mlmc.mc.x=MLMCUtil.mcPosXToMcPosX(fromMC, container, x);
			mlmc.mc.y=MLMCUtil.mcPosYToMcPosY(fromMC, container, y);
		}
		
		public function setText(text:String, show:Boolean=true, angleToCenterByMouse:Boolean=false):void {
			mc_tf.htmlText=text;
			mc_tf.width=mc_tf.textWidth+5;			mc_tf.height=mc_tf.textHeight+5;
			mc_bg.width=mc_tf.width+4+margin*2;			mc_bg.height=mc_tf.height+4+margin*2;
			if(angleToCenterByMouse){
				var stage:Stage=MILibMain.getInstance().getStage();
				setAlignByAngle(MLMathUtil.getAngleByPoints(stage.stageWidth/2, stage.mouseX, stage.stageHeight/2, stage.mouseY));
			}
			drawAlign();
			setIsVisible(show);
		}
		
		public function setAlign(align:Number):void {
			this.align=MLMathUtil.scopeRoll(0, 4, align);
			drawAlign();
		}
		
		public function setAlignByAngle(angle:Number):void {
			angle=MLMathUtil.scopeRoll(0, 360, angle);
			setAlign(((angle-45)/90)%4);
		}
		
		protected function drawAlign():void {
			mc_triangle.rotation=0;
			var align01:Number=align%1;
			mc_triangle.rotation=int(align)*90;
			
			if(align<1){
				mc_bg.y=-mc_bg.height-fromTriangleDistance;
				mc_bg.x=-(mc_bg.width-mc_triangle_width)*(1-align01)-mc_triangle_width/2;
			}else if(align<2){
				mc_bg.x=fromTriangleDistance;
				mc_bg.y=-(mc_bg.height-mc_triangle_width)*(1-align01)-mc_triangle_width/2;
			}else if(align<3){
				mc_bg.y=fromTriangleDistance;
				mc_bg.x=-(mc_bg.width-mc_triangle_width)*align01-mc_triangle_width/2;
			}else{
				mc_bg.x=-mc_bg.width-fromTriangleDistance;
				mc_bg.y=-(mc_bg.height-mc_triangle_width)*align01-mc_triangle_width/2;
			}
			mc_bg.x=int(mc_bg.x);
			mc_bg.y=int(mc_bg.y);
			mc_tf.x=mc_bg.x+mc_bg.width/2-mc_tf.width/2;			mc_tf.y=mc_bg.y+mc_bg.height/2-mc_tf.height/2;
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
//********************************************************************************************
//		events for MCTooltip
//********************************************************************************************
		protected function onMouseMove(event:MLEvent):void {
			setPosition(mc.parent.mouseX, mc.parent.mouseY);
		}
		
	}
}
